package com.lvtongka.app.ui.recharge

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.lvtongka.app.base.BaseViewModel
import com.lvtongka.app.common.bean.AccountRequest
import com.lvtongka.app.fragment.my.bean.MyInfo
import com.lvtongka.app.fragment.my.bean.MyResponse
import com.lvtongka.app.helper.SPHelper
import com.lvtongka.app.helper.SPKConstants
import com.lvtongka.app.http.HttpCallBack
import com.lvtongka.app.http.OkhttpUtils
import com.lvtongka.app.http.common.UrlConstants
import com.lvtongka.app.ui.recharge.bean.CardDelayData
import com.lvtongka.app.ui.recharge.bean.CardDelayRequest
import com.lvtongka.app.ui.recharge.bean.CardDelayResponse
import com.lvtongka.app.ui.recharge.bean.ChannelListData
import com.lvtongka.app.ui.recharge.bean.ChannelListResponse
import com.lvtongka.app.ui.recharge.bean.OcrData
import com.lvtongka.app.ui.recharge.bean.OcrRequest
import com.lvtongka.app.ui.recharge.bean.OcrResponse
import com.lvtongka.app.ui.recharge.bean.RechargeAlipayData
import com.lvtongka.app.ui.recharge.bean.RechargeAlipayRequest
import com.lvtongka.app.ui.recharge.bean.RechargeAlipayResponse
import com.lvtongka.app.ui.recharge.bean.RechargeCardRequest
import com.lvtongka.app.ui.recharge.bean.RechargeData
import com.lvtongka.app.ui.recharge.bean.RechargeRequest
import com.lvtongka.app.ui.recharge.bean.RechargeResponse

/**
 * describe:
 *
 * @author:ZhangWei(张伟)
 * date:2024/11/9
 * time:16:53
 */
class RechargeViewModel : BaseViewModel() {

    private val _rechargeData = MutableLiveData<RechargeData>()
    val rechargeData: LiveData<RechargeData> = _rechargeData

    private val _rechargeCard = MutableLiveData<Boolean>()
    val rechargeCard: LiveData<Boolean> = _rechargeCard

    private val _rechargeAlipay = MutableLiveData<RechargeAlipayData>()
    val rechargeAlipay: LiveData<RechargeAlipayData> = _rechargeAlipay

    private val _myInfo = MutableLiveData<MyInfo>()
    val myInfo: LiveData<MyInfo> = _myInfo

    private val _ocrData = MutableLiveData<OcrData>()
    val ocrData: LiveData<OcrData> = _ocrData

    private val _cardDelayData = MutableLiveData<CardDelayData>()
    val cardDelayData: LiveData<CardDelayData> = _cardDelayData

    private val _channelListData = MutableLiveData<ChannelListData>()
    val channelListData: LiveData<ChannelListData> = _channelListData

    // ocr识别
    fun getOcrInfo(pictureBase64: String) {
        val request = OcrRequest(pictureBase64)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val ocrResponse = Gson().fromJson(result, OcrResponse::class.java)
                    _ocrData.postValue(ocrResponse.data)
                }
            }, getAccessToken())
    }

    // 读取旅通卡信息
    fun getCardInfo(ltkCard: String, ltkPwd: String) {
        val request = RechargeRequest(ltkCard, ltkPwd)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val recharge = Gson().fromJson(result, RechargeResponse::class.java)
                    _rechargeData.postValue(recharge.data)
                }
            }, getAccessToken())
    }

    // 充值旅通卡
    fun rechargeCard(ltkCard: String, ltkPwd: String, rechargeAmt: Int) {
        val request = RechargeCardRequest(ltkCard, ltkPwd, rechargeAmt)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    _rechargeCard.postValue(true)
                }
            }, getAccessToken())
    }

    // 充值
    fun rechargePay(rechargeAmt: Int, payType: String) {
        val request = RechargeAlipayRequest(rechargeAmt, payType)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_RECHARGE, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val recharge = Gson().fromJson(result, RechargeAlipayResponse::class.java)
                    _rechargeAlipay.postValue(recharge.data)
                }
            }, getAccessToken())
    }

    fun getMyInfo() {
        val request = AccountRequest("03")
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    // 存储数据
                    SPHelper.putString(SPKConstants.BASIC_INFO, result)
                    val my = Gson().fromJson(result, MyResponse::class.java)
                    when(my.data.cell_bind_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_CELL_BIND, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_CELL_BIND, true)
                    }
                    when(my.data.real_name_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_REAL_NAME, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_REAL_NAME, true)
                    }
                    when(my.data.password_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_SET_PWD, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_SET_PWD, true)
                    }
                    when(my.data.func_scan_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_FUNC_SCAN, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_FUNC_SCAN, true)
                    }
                    when(my.data.no_password_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_NO_PASSWORD, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_NO_PASSWORD, true)
                    }
                    SPHelper.putInt(SPKConstants.MY_LIMIT_NO_PASSWORD, my.data.limit_no_password)
                    SPHelper.putString(SPKConstants.MY_CELL, my.data.cell)
                    SPHelper.putString(SPKConstants.MEMBER_ID, my.data.member_id)
                    _myInfo.postValue(my.data)
                }
            }, getAccessToken())
    }

    // 卡延期
    fun cardDelay(cardNo: String, pwd: String) {
        val request = CardDelayRequest(cardNo, pwd)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_CARD_DELAY, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val resp = Gson().fromJson(result, CardDelayResponse::class.java)
                    _cardDelayData.postValue(resp.data)
                }
            }, getAccessToken())
    }

    /**
     * 可支付渠道列表
     */
    fun getChannelList() {
        val map = HashMap<String, String>()
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(map), UrlConstants.API_CHANNEL_LIST, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val response = Gson().fromJson(result, ChannelListResponse::class.java)
                    // 存储数据
                    _channelListData.postValue(response.data)
                }
            }, getAccessToken())
    }
}